package cc.customer.myCustomer.services.impl;

import cc.customer.cAppNews.CAppNewsService;
import cc.customer.clue.service.ClueUtiService;
import cc.customer.expCustomer.dao.ExEmpMapper;
import cc.customer.expCustomer.domain.ExEmp;
import cc.customer.aiEmport.damain.CAiEmp;
import cc.customer.aiEmport.dao.CaiEmpMapper;
import cc.customer.bigEmport.damain.BigEmp;
import cc.customer.bigEmport.dao.BigEmpMapper;
import cc.customer.common.domain.ResponseBo;
import cc.customer.common.service.impl.BaseService;
import cc.customer.common.utils.Constant;
import cc.customer.common.utils.Utils;
import cc.customer.cusDynamic.dao.CusDynamicMapper;
import cc.customer.cusDynamic.domain.CusDynamic;
import cc.customer.cusDynamic.services.CusDynamicService;
import cc.customer.myCustomer.damain.CusResource;
import cc.customer.myCustomer.damain.EmpFenPei;
import cc.customer.myCustomer.damain.MyCustomer;
import cc.customer.myCustomer.dao.CusResourceMapper;
import cc.customer.myCustomer.dao.MyCustomerMapper;
import cc.customer.myCustomer.services.MyCustomerService;
import cc.customer.onschool.dao.OnSchoolResourceMapper;
import cc.customer.onschool.domain.OnSchoolResource;
import cc.customer.rabbit.domain.CAppNews;
import cc.customer.system.dao.SaleTagMapper;
import cc.customer.system.domain.MyUser;
import cc.customer.system.domain.SaleTag;
import cc.customer.system.service.UserService;
import cc.customer.utils.AutonCusInfo;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
@Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
public class MyCustomerServiceImpl extends BaseService<CusResource> implements MyCustomerService {

   private static final Logger log = LoggerFactory.getLogger(CusResource.class);

   @Autowired
   private CusResourceMapper cusResourceMapper;
   @Autowired
   private BigEmpMapper bigEmpMapper;
   @Autowired
   private OnSchoolResourceMapper onSchoolResourceMapper;
   @Autowired
   private ExEmpMapper exEmpMapper;
   @Autowired
   private MyCustomerMapper mcsMapper;
   @Autowired
   private CaiEmpMapper caiEmpMapper;
   @Autowired
   private SaleTagMapper saleTagMapper;
   @Autowired
   private UserService userMapper;
   @Autowired
   private CusDynamicMapper cusDynamicMapper;
   @Autowired
   private CusDynamicService cusDynamicService;
   @Autowired
   private ClueUtiService clueUtiService;

   @Autowired
   private CAppNewsService cAppNewsService;

   /**加载我的客户列表*/
   @Override
   public List<CusResource> selectMyCustomer(CusResource cusResource,MyUser users){
      try {
         /**查询包含标签**/
         String label = cusResource.getLabel();//包含标签
         if (!StringUtils.isEmpty(label)){
            List<String> list = Arrays.asList(label.split(","));
            cusResource.setContainLabel(list);
         }
         /**查询不包含标签**/
         String notLabel = cusResource.getNotLabel();
         if (!StringUtils.isEmpty(notLabel)){
            List<String> list = Arrays.asList(notLabel.split(","));
            cusResource.setNotcontainLabel(list);
         }
         /**时间格式化**/
         String starttime = cusResource.getBeforeTime();
         String endtime = cusResource.getAfterTime();
         if (!StringUtils.isEmpty(starttime) && !StringUtils.isEmpty(endtime)){
            String timestart = starttime+" 00:00:00";
            String timeend = endtime+" 23:59:59";
            cusResource.setBeforeTime(timestart); cusResource.setAfterTime(timeend);
         }
         List<CusResource> list  = cusResourceMapper.selectCusResourceList(cusResource);
         List<SaleTag> saleTags = saleTagMapper.selectAll();

         List<MyUser> myUsers = userMapper.selectAll();
         for (CusResource cusResourcelist:list){
            String labelselect = cusResourcelist.getLabel();
            if (!StringUtils.isEmpty(labelselect)){
               List<String> listStr = Arrays.asList(labelselect.split(","));
               List<String> listStrAtty = new ArrayList<>();
               for (String str: listStr){
                  if (!StringUtils.isEmpty(str)){
                     listStrAtty.add(str);
                  }
               }
               if (!listStrAtty.isEmpty()){
                  StringBuilder sb = new StringBuilder();
                  for (SaleTag tag:saleTags){
                        if (listStrAtty.contains(tag.getTagUuids())){
                           sb.append(tag.getTagName()+",");
                        }
                  }
                  int lastIndes = sb.lastIndexOf(",");
                  if (lastIndes >0){
                     cusResourcelist.setLabel(sb.substring(0,lastIndes));
                  }
               }
            }
            String userid = cusResourcelist.getDistributor();
            if (!StringUtils.isEmpty(userid)){
               List<String> userall = Arrays.asList(userid.split(","));
               StringBuilder sb = new StringBuilder();
               for (MyUser user :myUsers){
                  if (userall.contains(user.getUserUuid())){
                     sb.append(user.getNickname()+",");
                  }
               }
               int lastIndes = sb.lastIndexOf(",");
               if (lastIndes >0){
                  cusResourcelist.setDistributor(sb.substring(0,lastIndes));
               }
            }
        AutonCusInfo.parseResource(cusResourcelist,users);
         }
         return list;
      }catch (Exception e){
         e.printStackTrace();
         return new ArrayList<>();
      }
   }

   /**放弃我的客户*/
   //TODO("在这里增加销售助理推送消息 建议推送全公司人员")
   @Override
   public ResponseBo giveUpMyCustomer(String keys, String types,String userid,String names){
      try {
         String[] keyids = keys.split(",");
         String[] typeids = types.split(",");
         //推广资源
         List<String> listex = new ArrayList<>();
         //大资源
         List<String> listbig = new ArrayList<>();
         //网校资源
         List<String> liston = new ArrayList<>();
         //我的客户
         List<String> listmy = new ArrayList<>();
         //Ai资源
         List<String> listAo = new ArrayList<>();

         for(int t = 0;t < typeids.length;t++){
            if(Constant.ZIYUAN_TYPE_EXEMP.equals(typeids[t])){
               listex.add(keyids[t]);
            }else if(Constant.ZIYUAN_TYPE_SCHOLLE.equals(typeids[t])){
               liston.add(keyids[t]);
            }else if(Constant.ZIYUAN_TYPE_BIGENP.equals(typeids[t])){
               listbig.add(keyids[t]);
            }else if(Constant.ZIYUAN_TYPE_MINE.equals(typeids[t])){
               listmy.add(keyids[t]);
            }else if (Constant.ZIYUAN_TYPE_AI.equals(typeids[t])){
               listAo.add(keyids[t]);
            }
         }
         //AI资源放弃
         if(!listAo.isEmpty()){
            caiEmpMapper.updateBatchList(listAo);
            CusDynamic cus = new CusDynamic();
            cus.setDyCreator(userid);
            cus.setDySourceType(Constant.ZIYUAN_TYPE_AI);
            cus.setDyType(Constant.CUSTOMER_RECORD_GIVEUP);
            cusDynamicService.inserDynamic(listAo,cus);
         }
         //推广资源放弃
         if(!listex.isEmpty()){
            exEmpMapper.updateBatchList(listex);
            CusDynamic cus = new CusDynamic();
            cus.setDyCreator(userid);
            cus.setDySourceType(Constant.ZIYUAN_TYPE_EXEMP);
            cus.setDyType(Constant.CUSTOMER_RECORD_GIVEUP);
            cusDynamicService.inserDynamic(listex,cus);
         }
         //网校资源
         if(!liston.isEmpty()){
            onSchoolResourceMapper.updateBatchList(liston);
            CusDynamic cus = new CusDynamic();
            cus.setDyCreator(userid);
            cus.setDySourceType(Constant.ZIYUAN_TYPE_SCHOLLE);
            cus.setDyType(Constant.CUSTOMER_RECORD_GIVEUP);
            cusDynamicService.inserDynamic(liston,cus);
         }
         //大资源
         if (!listbig.isEmpty()){
            bigEmpMapper.updateBatchList(listbig);
            CusDynamic cus = new CusDynamic();
            cus.setDyCreator(userid);
            cus.setDySourceType(Constant.ZIYUAN_TYPE_BIGENP);
            cus.setDyType(Constant.CUSTOMER_RECORD_GIVEUP);
            cusDynamicService.inserDynamic(listbig,cus);
         }
         /**我的客户放弃*/
         if(!listmy.isEmpty()){
            mcsMapper.updateBatchList(listmy);
            CusDynamic cus = new CusDynamic();
            cus.setDyCreator(userid);
            cus.setDySourceType(Constant.ZIYUAN_TYPE_MINE);
            cus.setDyType(Constant.CUSTOMER_RECORD_GIVEUP);
            cusDynamicService.inserDynamic(listmy,cus);
         }
         return ResponseBo.ok("放弃成功");
      }catch (Exception e){
         e.printStackTrace();
         return ResponseBo.error("放弃失败");
      }
   }

   /**共享我的客户*/
   //TODO("在这里增加销售助理推送消息 建议推送全公司人员")
   @Override
   @Transactional
   public ResponseBo shareMyCustomer(CusResource cus,String flag,String userid,String names){

      List<ExEmp> exlist = new ArrayList<>();//推广资源
      List<String> exuuids = new ArrayList<>();

      List<OnSchoolResource> oslist = new ArrayList<>(); //高校资源
      List<String> onuuids = new ArrayList<>();

      List<BigEmp> biglist = new ArrayList<>(); // 大资源客户
      List<String> biguuids = new ArrayList<>();

      List<MyCustomer> mylist = new ArrayList<>();//我的客户
      List<String> myuuids = new ArrayList<>();

      List<CAiEmp> cAiEmps = new ArrayList<>();
      List<String> cuuids = new ArrayList<>();

      try {
         List<String> uuidids = Arrays.asList(cus.getUuid().split(","));
         List<String> typeids = Arrays.asList(cus.getTypes().split(","));
         List<String> ids = Arrays.asList((cus.getIds()+"").split(","));
         for(int t = 0;t < typeids.size();t++){
            String  typeid = typeids.get(t);
            if (Constant.ZIYUAN_TYPE_AI.equals(typeid)){
               //Ai资源
               CAiEmp cAiEmp = new CAiEmp();
               cAiEmp.setAiId(Long.valueOf(ids.get(t)));
               cAiEmp.setAiRUuid(uuidids.get(t));
               cAiEmp.setAiMyCustomer(","+cus.getSharcus());
               cAiEmps.add(cAiEmp);
               cuuids.add(ids.get(t));
            }else if(Constant.ZIYUAN_TYPE_EXEMP.equals(typeid)){
               //推广资源
               ExEmp exEmp = new ExEmp();
               exEmp.setcExId(Integer.valueOf(ids.get(t)));
               exEmp.setcExUuid(uuidids.get(t));
               exEmp.setcExCustomer(","+cus.getSharcus());
               exlist.add(exEmp);
               exuuids.add(ids.get(t));
            }else if (Constant.ZIYUAN_TYPE_SCHOLLE.equals(typeid)){
               //网校资源
               OnSchoolResource onclaole = new OnSchoolResource();
               onclaole.setOsId(Long.valueOf(ids.get(t)));
               onclaole.setOsUuid(uuidids.get(t));
               onclaole.setOsCusShare(","+cus.getSharcus());
               oslist.add(onclaole);
               onuuids.add(ids.get(t));
            }else if(Constant.ZIYUAN_TYPE_BIGENP.equals(typeid)){
               //大资源类型
               BigEmp bigEmp = new BigEmp();
               bigEmp.setBpRUuid(uuidids.get(t));
               bigEmp.setBpId(Long.valueOf(ids.get(t)));
               bigEmp.setBpMyCustomer(","+cus.getSharcus());
               biglist.add(bigEmp);
               biguuids.add(ids.get(t));
            }else if(Constant.ZIYUAN_TYPE_MINE.equals(typeid)){
               MyCustomer customer = new MyCustomer();
               customer.setMyCusId(Long.valueOf(ids.get(t)));
               customer.setMyCusUuid(uuidids.get(t));
               customer.setMyCusShare(","+cus.getSharcus());
               mylist.add(customer);
               myuuids.add(ids.get(t));
            }
         }

         //更新AI资源类型
         if (!cAiEmps.isEmpty()){
            if ("1".equals(flag)){
               //保持原有得共享关系,
               caiEmpMapper.updateCusBatchContact(cAiEmps);
            }else{
               caiEmpMapper.updateCusBatchTi(cAiEmps);
            }
            CusDynamic cusde = new CusDynamic();
            cusde.setDyCreator(userid);
            cusde.setDySourceType(Constant.ZIYUAN_TYPE_AI);
            cusde.setDyType(Constant.CUSTOMER_RECORD_SHARCUSTOMER);
            cusde.setDyShareUserid(cus.getSharcus());
            cusDynamicService.inserDynamic(cuuids,cusde);
         }

         //更新推广资源类型
         if (!exlist.isEmpty()){
            if ("1".equals(flag)){
               //保持原有得共享关系,
               exEmpMapper.updateCusBatchContact(exlist);
            }else{
               exEmpMapper.updateCusBatchTi(exlist);
            }
            CusDynamic cusde = new CusDynamic();
            cusde.setDyCreator(userid);
            cusde.setDySourceType(Constant.ZIYUAN_TYPE_EXEMP);
            cusde.setDyType(Constant.CUSTOMER_RECORD_SHARCUSTOMER);
            cusde.setDyShareUserid(cus.getSharcus());
            cusDynamicService.inserDynamic(exuuids,cusde);
         }
         //更新网校资源
         if(!oslist.isEmpty()){
            if ("1".equals(flag)){
               //保持原有得共享关系,
               onSchoolResourceMapper.updateCusBatchContactSchol(oslist);
            }else{
               onSchoolResourceMapper.updateCusBatchTiSchol(oslist);
            }
            CusDynamic cusde = new CusDynamic();
            cusde.setDyCreator(userid);
            cusde.setDySourceType(Constant.ZIYUAN_TYPE_SCHOLLE);
            cusde.setDyType(Constant.CUSTOMER_RECORD_SHARCUSTOMER);
            cusde.setDyShareUserid(cus.getSharcus());
            cusDynamicService.inserDynamic(onuuids,cusde);
         }
         //更新大资源类型
         if (!biglist.isEmpty()){
            if ("1".equals(flag)){
               //保持原有得共享关系,
               bigEmpMapper.updateCusBatchContactBig(biglist);
            }else{
               bigEmpMapper.updateCusBatchTiBig(biglist);
            }
            CusDynamic cusde = new CusDynamic();
            cusde.setDyCreator(userid);
            cusde.setDySourceType(Constant.ZIYUAN_TYPE_BIGENP);
            cusde.setDyType(Constant.CUSTOMER_RECORD_SHARCUSTOMER);
            cusde.setDyShareUserid(cus.getSharcus());
            cusDynamicService.inserDynamic(biguuids,cusde);
         }
         if(!mylist.isEmpty()){
            if ("1".equals(flag)){
               //保持原有得共享关系,
               mcsMapper.updateCusBatchContactMine(mylist);
            }else{
               mcsMapper.updateCusBatchTiMine(mylist);
            }
            CusDynamic cusde = new CusDynamic();
            cusde.setDyCreator(userid);
            cusde.setDySourceType(Constant.ZIYUAN_TYPE_MINE);
            cusde.setDyType(Constant.CUSTOMER_RECORD_SHARCUSTOMER);
            cusde.setDyShareUserid(cus.getSharcus());
            cusDynamicService.inserDynamic(myuuids,cusde);
         }
         return ResponseBo.ok("分享成功");
      }catch (Exception e){
         e.printStackTrace();
         return ResponseBo.error("分享失败");
      }
   }

   /**共享我的客户*/
   //TODO("在这里增加销售助理推送消息 建议推送全公司人员")
   @Override
   @Transactional
   public ResponseBo shareAppMyCustomer(CusResource cus,String flag,String userid,String names){

      List<ExEmp> exlist = new ArrayList<>();//推广资源
      List<String> exuuids = new ArrayList<>();

      List<OnSchoolResource> oslist = new ArrayList<>(); //高校资源
      List<String> onuuids = new ArrayList<>();

      List<BigEmp> biglist = new ArrayList<>(); // 大资源客户
      List<String> biguuids = new ArrayList<>();

      List<MyCustomer> mylist = new ArrayList<>();//我的客户
      List<String> myuuids = new ArrayList<>();

      List<CAiEmp> cAiEmps = new ArrayList<>();
      List<String> cuuids = new ArrayList<>();

      try {
         List<String> uuidids = Arrays.asList(cus.getUuid().split(","));
         List<String> typeids = Arrays.asList(cus.getTypes().split(","));
         List<String> ids = Arrays.asList((cus.getIds()+"").split(","));
         for(int t = 0;t < typeids.size();t++){
            String  typeid = typeids.get(t);
            if (Constant.ZIYUAN_TYPE_AI.equals(typeid)){
               //Ai资源
               CAiEmp cAiEmp = new CAiEmp();
               cAiEmp.setAiId(Long.valueOf(ids.get(t)));
               cAiEmp.setAiRUuid(uuidids.get(t));
               cAiEmp.setAiMyCustomer(","+cus.getSharcus());
               cAiEmps.add(cAiEmp);
               cuuids.add(ids.get(t));

            }else if(Constant.ZIYUAN_TYPE_EXEMP.equals(typeid)){
               //推广资源
               ExEmp exEmp = new ExEmp();
               exEmp.setcExId(Integer.valueOf(ids.get(t)));
               exEmp.setcExUuid(uuidids.get(t));
               exEmp.setcExCustomer(","+cus.getSharcus());
               exlist.add(exEmp);
               exuuids.add(ids.get(t));
            }else if (Constant.ZIYUAN_TYPE_SCHOLLE.equals(typeid)){
               //网校资源
               OnSchoolResource onclaole = new OnSchoolResource();
               onclaole.setOsId(Long.valueOf(ids.get(t)));
               onclaole.setOsUuid(uuidids.get(t));
               onclaole.setOsCusShare(","+cus.getSharcus());
               oslist.add(onclaole);
               onuuids.add(ids.get(t));
            }else if(Constant.ZIYUAN_TYPE_BIGENP.equals(typeid)){
               //大资源类型
               BigEmp bigEmp = new BigEmp();
               bigEmp.setBpRUuid(uuidids.get(t));
               bigEmp.setBpId(Long.valueOf(ids.get(t)));
               bigEmp.setBpMyCustomer(","+cus.getSharcus());
               biglist.add(bigEmp);
               biguuids.add(ids.get(t));
            }else if(Constant.ZIYUAN_TYPE_MINE.equals(typeid)){
               MyCustomer customer = new MyCustomer();
               customer.setMyCusId(Long.valueOf(ids.get(t)));
               customer.setMyCusUuid(uuidids.get(t));
               customer.setMyCusShare(","+cus.getSharcus());
               mylist.add(customer);
               myuuids.add(ids.get(t));
            }
         }

         //更新AI资源类型
         if (!cAiEmps.isEmpty()){
            if ("1".equals(flag)){
               //保持原有得共享关系,
               caiEmpMapper.updateCusBatchContact(cAiEmps);
            }else{
               caiEmpMapper.updateCusBatchTi(cAiEmps);
            }
            CusDynamic cusde = new CusDynamic();
            cusde.setDyCreator(userid);
            cusde.setDySourceType(Constant.ZIYUAN_TYPE_AI);
            cusde.setDyType(Constant.CUSTOMER_RECORD_SHARCUSTOMER);
            cusde.setDyShareUserid(cus.getSharcus());
            cusDynamicService.inserDynamic(cuuids,cusde);

         }

         //更新推广资源类型
         if (!exlist.isEmpty()){
            if ("1".equals(flag)){
               //保持原有得共享关系,
               exEmpMapper.updateCusBatchContact(exlist);
            }else{
               exEmpMapper.updateCusBatchTi(exlist);
            }
            CusDynamic cusde = new CusDynamic();
            cusde.setDyCreator(userid);
            cusde.setDySourceType(Constant.ZIYUAN_TYPE_EXEMP);
            cusde.setDyType(Constant.CUSTOMER_RECORD_SHARCUSTOMER);
            cusde.setDyShareUserid(cus.getSharcus());
            cusDynamicService.inserDynamic(exuuids,cusde);
         }
         //更新网校资源
         if(!oslist.isEmpty()){
            if ("1".equals(flag)){
               //保持原有得共享关系,
               onSchoolResourceMapper.updateCusBatchContactSchol(oslist);
            }else{
               onSchoolResourceMapper.updateCusBatchTiSchol(oslist);
            }
            CusDynamic cusde = new CusDynamic();
            cusde.setDyCreator(userid);
            cusde.setDySourceType(Constant.ZIYUAN_TYPE_SCHOLLE);
            cusde.setDyType(Constant.CUSTOMER_RECORD_SHARCUSTOMER);
            cusde.setDyShareUserid(cus.getSharcus());
            cusDynamicService.inserDynamic(onuuids,cusde);
         }
         //更新大资源类型
         if (!biglist.isEmpty()){
            if ("1".equals(flag)){
               //保持原有得共享关系,
               bigEmpMapper.updateCusBatchContactBig(biglist);
            }else{
               bigEmpMapper.updateCusBatchTiBig(biglist);
            }
            CusDynamic cusde = new CusDynamic();
            cusde.setDyCreator(userid);
            cusde.setDySourceType(Constant.ZIYUAN_TYPE_BIGENP);
            cusde.setDyType(Constant.CUSTOMER_RECORD_SHARCUSTOMER);
            cusde.setDyShareUserid(cus.getSharcus());
            cusDynamicService.inserDynamic(biguuids,cusde);
         }
         if(!mylist.isEmpty()){
            if ("1".equals(flag)){
               //保持原有得共享关系,
               mcsMapper.updateCusBatchContactMine(mylist);
            }else{
               mcsMapper.updateCusBatchTiMine(mylist);
            }
            CusDynamic cusde = new CusDynamic();
            cusde.setDyCreator(userid);
            cusde.setDySourceType(Constant.ZIYUAN_TYPE_MINE);
            cusde.setDyType(Constant.CUSTOMER_RECORD_SHARCUSTOMER);
            cusde.setDyShareUserid(cus.getSharcus());
            cusDynamicService.inserDynamic(myuuids,cusde);
         }
         return ResponseBo.ok("分享成功");
      }catch (Exception e){
         e.printStackTrace();
         return ResponseBo.error("分享失败");
      }
   }
   /**转让我得客户**/
   @Override
   @Transactional
   public ResponseBo MyCustomerTransfer(CusResource cusResource,String userid) throws RuntimeException{

      List<ExEmp> exlist = new ArrayList<>();//推广资源
      List<String> exuuids = new ArrayList<>();

      List<OnSchoolResource> oslist = new ArrayList<>(); //高校资源
      List<String> onuuids = new ArrayList<>();

      List<BigEmp> biglist = new ArrayList<>(); // 大资源客户
      List<String> biguuids = new ArrayList<>();

      List<MyCustomer> mylist = new ArrayList<>();//我的客户
      List<String> myuuids = new ArrayList<>();

      List<CAiEmp> cAiEmps = new ArrayList<>();
      List<String> caUuid = new ArrayList<>();

      try {
         List<String> uuidids = Arrays.asList(cusResource.getUuid().split(","));
         List<String>  typeids = Arrays.asList(cusResource.getTypes().split(","));
         List<String> ids = Arrays.asList((cusResource.getIds()).split(","));
         for(int t = 0;t < typeids.size();t++){
            String  typeid = typeids.get(t);
            if (Constant.ZIYUAN_TYPE_AI.equals(typeid)){
               CAiEmp cAiEmp = new CAiEmp();
               cAiEmp.setAiId(Long.valueOf(ids.get(t)));
               cAiEmp.setAiRUuid(uuidids.get(t));
               cAiEmp.setAiDistriute(cusResource.getReceiver());
               cAiEmps.add(cAiEmp);
               caUuid.add(ids.get(t));
            }else if(Constant.ZIYUAN_TYPE_EXEMP.equals(typeid)){
               //推广资源
               ExEmp exEmp = new ExEmp();
               exEmp.setcExId(Integer.valueOf(ids.get(t)));
               exEmp.setcExUuid(uuidids.get(t));
               exEmp.setcExReceiver(cusResource.getReceiver());
               MyUser user = userMapper.findUserByUuids(cusResource.getReceiver());
               if (user!=null){
                  exEmp.setcExDept(user.getDeptId()+"");
               }
               exlist.add(exEmp);
               exuuids.add(ids.get(t));
            }else if (Constant.ZIYUAN_TYPE_SCHOLLE.equals(typeid)){
               //网校资源
               OnSchoolResource onclaole = new OnSchoolResource();
               onclaole.setOsId(Long.valueOf(ids.get(t)));
               onclaole.setOsUuid(uuidids.get(t));
               onclaole.setOsMyCustomer(cusResource.getReceiver());
               MyUser user = userMapper.findUserByUuids(cusResource.getReceiver());
               if (user!=null){
                  onclaole.setOsDept(user.getDeptId()+"");
               }
               oslist.add(onclaole);
               onuuids.add(ids.get(t));
            }else if(Constant.ZIYUAN_TYPE_BIGENP.equals(typeid)){
               //大资源类型
               BigEmp bigEmp = new BigEmp();
               bigEmp.setBpRUuid(uuidids.get(t));
               bigEmp.setBpId(Long.valueOf(ids.get(t)));
               bigEmp.setBpDistribute(cusResource.getReceiver());
               biglist.add(bigEmp);
               biguuids.add(ids.get(t));
            }else if(Constant.ZIYUAN_TYPE_MINE.equals(typeid)){
               MyCustomer customer = new MyCustomer();
               customer.setMyCusId(Long.valueOf(ids.get(t)));
               customer.setMyCusUuid(uuidids.get(t));
               customer.setMyCusReceiver(cusResource.getReceiver());
               MyUser user = userMapper.findUserByUuids(cusResource.getReceiver());
               if (user!=null){
                  customer.setMyCusDept(user.getDeptId()+"");
               }
               mylist.add(customer);
               myuuids.add(ids.get(t));
            }
         }
         //更新AI资源类型
         if (!cAiEmps.isEmpty()){
            caiEmpMapper.updateAiSeaBigEmpTransfer(cAiEmps);
            CusDynamic cusde = new CusDynamic();
            cusde.setDyCreator(userid);
            cusde.setDySourceType(Constant.ZIYUAN_TYPE_AI);
            cusde.setDyType(Constant.CUSTOMER_RECORD_ZHUANRQANG);
            cusde.setDyZhuanUserid(cusResource.getReceiver());
            cusDynamicService.inserDynamic(caUuid,cusde);
         }
         //更新推广资源类型
         if (!exlist.isEmpty()){
            exEmpMapper.updateBigExTransfer(exlist);
            CusDynamic cusde = new CusDynamic();
            cusde.setDyCreator(userid);
            cusde.setDySourceType(Constant.ZIYUAN_TYPE_EXEMP);
            cusde.setDyType(Constant.CUSTOMER_RECORD_ZHUANRQANG);
            cusde.setDyZhuanUserid(cusResource.getReceiver());
            cusDynamicService.inserDynamic(exuuids,cusde);
         }
         //更新网校资源
         if(!oslist.isEmpty()){
            onSchoolResourceMapper.updateBigSeaScholTransfer(oslist);
            CusDynamic cusde = new CusDynamic();
            cusde.setDyCreator(userid);
            cusde.setDySourceType(Constant.ZIYUAN_TYPE_SCHOLLE);
            cusde.setDyType(Constant.CUSTOMER_RECORD_ZHUANRQANG);
            cusde.setDyZhuanUserid(cusResource.getReceiver());
            cusDynamicService.inserDynamic(onuuids,cusde);
         }
         //更新大资源类型
         if (!biglist.isEmpty()){
            bigEmpMapper.updateBigSeaBigEmpTransfer(biglist);
            CusDynamic cusde = new CusDynamic();
            cusde.setDyCreator(userid);
            cusde.setDySourceType(Constant.ZIYUAN_TYPE_BIGENP);
            cusde.setDyType(Constant.CUSTOMER_RECORD_ZHUANRQANG);
            cusde.setDyZhuanUserid(cusResource.getReceiver());
            cusDynamicService.inserDynamic(biguuids,cusde);
         }
         if(!mylist.isEmpty()){
            mcsMapper.updateBigSeaMyTransfer(mylist);
            CusDynamic cusde = new CusDynamic();
            cusde.setDyCreator(userid);
            cusde.setDySourceType(Constant.ZIYUAN_TYPE_MINE);
            cusde.setDyType(Constant.CUSTOMER_RECORD_ZHUANRQANG);
            cusde.setDyZhuanUserid(cusResource.getReceiver());
            cusDynamicService.inserDynamic(myuuids,cusde);
         }
         return ResponseBo.ok("转让成功");
      }catch (Exception e){
         e.printStackTrace();
         return ResponseBo.error("转让失败");
      }
   }
   /**app转让我得客户**/
   @Override
   @Transactional
   public ResponseBo MyAppCustomerTransfer(CusResource cusResource,String userid) throws RuntimeException{

      List<ExEmp> exlist = new ArrayList<>();//推广资源
      List<String> exuuids = new ArrayList<>();

      List<OnSchoolResource> oslist = new ArrayList<>(); //高校资源
      List<String> onuuids = new ArrayList<>();

      List<BigEmp> biglist = new ArrayList<>(); // 大资源客户
      List<String> biguuids = new ArrayList<>();

      List<MyCustomer> mylist = new ArrayList<>();//我的客户
      List<String> myuuids = new ArrayList<>();

      List<CAiEmp> cAiEmps = new ArrayList<>();
      List<String> caUuid = new ArrayList<>();

      try {
         List<String> uuidids = Arrays.asList(cusResource.getUuid().split(","));
         List<String>  typeids = Arrays.asList(cusResource.getTypes().split(","));
         List<String> ids = Arrays.asList((cusResource.getIds()).split(","));
         for(int t = 0;t < typeids.size();t++){
            String  typeid = typeids.get(t);
            if (Constant.ZIYUAN_TYPE_AI.equals(typeid)){
               CAiEmp cAiEmp = new CAiEmp();
               cAiEmp.setAiId(Long.valueOf(ids.get(t)));
               cAiEmp.setAiRUuid(uuidids.get(t));
               cAiEmp.setAiDistriute(cusResource.getReceiver());
               cAiEmps.add(cAiEmp);
               caUuid.add(ids.get(t));
            }else if(Constant.ZIYUAN_TYPE_EXEMP.equals(typeid)){
               //推广资源
               ExEmp exEmp = new ExEmp();
               exEmp.setcExId(Integer.valueOf(ids.get(t)));
               exEmp.setcExUuid(uuidids.get(t));
               exEmp.setcExReceiver(cusResource.getReceiver());
               MyUser user = userMapper.findUserByUuids(cusResource.getReceiver());
               if (user!=null){
                  exEmp.setcExDept(user.getDeptId()+"");
               }
               exlist.add(exEmp);
               exuuids.add(ids.get(t));
            }else if (Constant.ZIYUAN_TYPE_SCHOLLE.equals(typeid)){
               //网校资源
               OnSchoolResource onclaole = new OnSchoolResource();
               onclaole.setOsId(Long.valueOf(ids.get(t)));
               onclaole.setOsUuid(uuidids.get(t));
               onclaole.setOsMyCustomer(cusResource.getReceiver());
               MyUser user = userMapper.findUserByUuids(cusResource.getReceiver());
               if (user!=null){
                  onclaole.setOsDept(user.getDeptId()+"");
               }
               oslist.add(onclaole);
               onuuids.add(ids.get(t));
            }else if(Constant.ZIYUAN_TYPE_BIGENP.equals(typeid)){
               //大资源类型
               BigEmp bigEmp = new BigEmp();
               bigEmp.setBpRUuid(uuidids.get(t));
               bigEmp.setBpId(Long.valueOf(ids.get(t)));
               bigEmp.setBpDistribute(cusResource.getReceiver());
               biglist.add(bigEmp);
               biguuids.add(ids.get(t));
            }else if(Constant.ZIYUAN_TYPE_MINE.equals(typeid)){
               MyCustomer customer = new MyCustomer();
               customer.setMyCusId(Long.valueOf(ids.get(t)));
               customer.setMyCusUuid(uuidids.get(t));
               customer.setMyCusReceiver(cusResource.getReceiver());
               MyUser user = userMapper.findUserByUuids(cusResource.getReceiver());
               if (user!=null){
                  customer.setMyCusDept(user.getDeptId()+"");
               }
               mylist.add(customer);
               myuuids.add(ids.get(t));
            }
         }
         //更新AI资源类型
         if (!cAiEmps.isEmpty()){
            caiEmpMapper.updateAiSeaBigEmpTransfer(cAiEmps);
            CusDynamic cusde = new CusDynamic();
            cusde.setDyCreator(userid);
            cusde.setDySourceType(Constant.ZIYUAN_TYPE_AI);
            cusde.setDyType(Constant.CUSTOMER_RECORD_ZHUANRQANG);
            cusde.setDyZhuanUserid(cusResource.getReceiver());
            cusDynamicService.inserDynamic(caUuid,cusde);
         }
         //更新推广资源类型
         if (!exlist.isEmpty()){
            exEmpMapper.updateBigExTransfer(exlist);
            CusDynamic cusde = new CusDynamic();
            cusde.setDyCreator(userid);
            cusde.setDySourceType(Constant.ZIYUAN_TYPE_EXEMP);
            cusde.setDyType(Constant.CUSTOMER_RECORD_ZHUANRQANG);
            cusde.setDyZhuanUserid(cusResource.getReceiver());
            cusDynamicService.inserDynamic(exuuids,cusde);
         }
         //更新网校资源
         if(!oslist.isEmpty()){
            onSchoolResourceMapper.updateBigSeaScholTransfer(oslist);
            CusDynamic cusde = new CusDynamic();
            cusde.setDyCreator(userid);
            cusde.setDySourceType(Constant.ZIYUAN_TYPE_SCHOLLE);
            cusde.setDyType(Constant.CUSTOMER_RECORD_ZHUANRQANG);
            cusde.setDyZhuanUserid(cusResource.getReceiver());
            cusDynamicService.inserDynamic(onuuids,cusde);
         }
         //更新大资源类型
         if (!biglist.isEmpty()){
            bigEmpMapper.updateBigSeaBigEmpTransfer(biglist);
            CusDynamic cusde = new CusDynamic();
            cusde.setDyCreator(userid);
            cusde.setDySourceType(Constant.ZIYUAN_TYPE_BIGENP);
            cusde.setDyType(Constant.CUSTOMER_RECORD_ZHUANRQANG);
            cusde.setDyZhuanUserid(cusResource.getReceiver());
            cusDynamicService.inserDynamic(biguuids,cusde);
         }
         if(!mylist.isEmpty()){
            mcsMapper.updateBigSeaMyTransfer(mylist);
            CusDynamic cusde = new CusDynamic();
            cusde.setDyCreator(userid);
            cusde.setDySourceType(Constant.ZIYUAN_TYPE_MINE);
            cusde.setDyType(Constant.CUSTOMER_RECORD_ZHUANRQANG);
            cusde.setDyZhuanUserid(cusResource.getReceiver());
            cusDynamicService.inserDynamic(myuuids,cusde);
         }
         return ResponseBo.ok("转让成功");
      }catch (Exception e){
         e.printStackTrace();
         return ResponseBo.error("转让失败");
      }
   }

   /**我得客户设置标签**/
   @Override
   @Transactional
   public ResponseBo myCustomerLabel(CusResource cusResource,String pid,String idstr,String userid) throws RuntimeException{
      List<ExEmp> exlist = new ArrayList<>();//推广资源
      List<String> exuuids = new ArrayList<>();

      List<OnSchoolResource> oslist = new ArrayList<>(); //高校资源
      List<String> onuuids = new ArrayList<>();

      List<BigEmp> biglist = new ArrayList<>(); // 大资源客户
      List<String> biguuids = new ArrayList<>();

      List<MyCustomer> mylist = new ArrayList<>();//我的客户
      List<String> myuuids = new ArrayList<>();

      List<CAiEmp> cAiEmps = new ArrayList<>();  //AI资源
      List<String> cAiUuid = new ArrayList<>();
      try {
         List<String>  typeids = Arrays.asList(cusResource.getTypes().split(","));
         List<String> ids = Arrays.asList((cusResource.getIds()).split(","));
         for(int t = 0;t < typeids.size();t++){
            String  typeid = typeids.get(t);
            List<String> idstrids = Arrays.asList(idstr.split(","));
            List<String> tagid = Arrays.asList(pid.split(","));
            if(Constant.ZIYUAN_TYPE_EXEMP.equals(typeid)){
               //查询当前得推广资源得信息
               ExEmp emp = exEmpMapper.selectByPrimaryKey(Integer.valueOf(ids.get(t)));
               String curentLabel  = emp.getcExLabel();
               if (!StringUtils.isEmpty(curentLabel)){
                  StringBuilder sb = parseLableinfo(curentLabel,idstrids,tagid);
                  sb.append(cusResource.getLabel());
                  emp.setcExLabel(sb.toString());
                  emp.setcExLabelName(curentLabel);
               }else{
                  emp.setcExLabel(cusResource.getLabel());
               }
               exlist.add(emp);
               exuuids.add(ids.get(t));
            }else if (Constant.ZIYUAN_TYPE_SCHOLLE.equals(typeid)){
               //查询当前得推广资源得信息
               OnSchoolResource sacool = onSchoolResourceMapper.selectByPrimaryKey(Long.valueOf(ids.get(t)));
               String curentLabel  = sacool.getOsLabel();
               if (!StringUtils.isEmpty(curentLabel)){
                  StringBuilder sb = parseLableinfo(curentLabel,idstrids,tagid);
                  sb.append(cusResource.getLabel());
                  sacool.setOsLabel(sb.toString());
                  sacool.setOsLabelName(curentLabel);
               }else{ sacool.setOsLabel(cusResource.getLabel()); }
               oslist.add(sacool);
               onuuids.add(ids.get(t));
            }else if(Constant.ZIYUAN_TYPE_BIGENP.equals(typeid)){
               //查询当前得推广资源得信息
               BigEmp bigEmp = bigEmpMapper.selectByPrimaryKey(Long.valueOf(ids.get(t)));
               String curentLabel  = bigEmp.getBpLabel();
               if (!StringUtils.isEmpty(curentLabel)){
                  StringBuilder sb = parseLableinfo(curentLabel,idstrids,tagid);
                  sb.append(cusResource.getLabel());
                  bigEmp.setBpLabel(sb.toString());
                  bigEmp.setBpLabelName(curentLabel);
               }else{
                  bigEmp.setBpLabel(cusResource.getLabel());
               }
               biglist.add(bigEmp);
               biguuids.add(ids.get(t));
            }else if(Constant.ZIYUAN_TYPE_MINE.equals(typeid)){
               //查询当前得个人资源得信息
               MyCustomer myCustomer = mcsMapper.selectByPrimaryKey(Long.valueOf(ids.get(t)));
               String curentLabel  = myCustomer.getMyCusLabel();
               if (!StringUtils.isEmpty(curentLabel)){
                  StringBuilder sb = parseLableinfo(curentLabel,idstrids,tagid);
                  sb.append(cusResource.getLabel());
                  myCustomer.setMyCusLabel(sb.toString());
                  myCustomer.setMyCusLabelName(curentLabel);
               }else{
                  myCustomer.setMyCusLabel(cusResource.getLabel());
               }
               mylist.add(myCustomer);
               myuuids.add(ids.get(t));
            }else if (Constant.ZIYUAN_TYPE_AI.equals(typeid)){

            //查询当前得个人资源得信息
               CAiEmp cAiEmp = caiEmpMapper.selectByPrimaryKey(Long.valueOf(ids.get(t)));
               String curentLabel  = cAiEmp.getAiLabel();
               if (!StringUtils.isEmpty(curentLabel)){
                  StringBuilder sb = parseLableinfo(curentLabel,idstrids,tagid);
                  sb.append(cusResource.getLabel());
                  cAiEmp.setAiLabel(sb.toString());
                  cAiEmp.setAiLabelName(curentLabel);
               }else{
                  cAiEmp.setAiLabel(cusResource.getLabel());
               }
                cAiEmps.add(cAiEmp);
                cAiUuid .add(ids.get(t));
            }
         }
         //更新推广资源类型
         if (!exlist.isEmpty()){
            exEmpMapper.updateCusBatchContactLabel(exlist);
            List<CusDynamic> list = new ArrayList<>();
            for (ExEmp exEmp : exlist){
               CusDynamic cusde = new CusDynamic();
               cusde.setDyCreator(userid);
               cusde.setDyUuid(Utils.getUUID());
               cusde.setDySourceType(Constant.ZIYUAN_TYPE_EXEMP);
               cusde.setDyType(Constant.CUSTOMER_RECORD_UPDATECUSTONER);
               cusde.setDyLabelAfter(cusResource.getLabel());
               cusde.setDyLabelBefore(exEmp.getcExLabelName());
               cusde.setDyCusUuid(exEmp.getcExId()+"");
               cusde.setDyContent(Constant.CUSTOMER_RECORD_UPDATECUSTONER_TEXT);
               list.add(cusde);
               clueUtiService.parseAutograph(cusde);
            }
            cusDynamicMapper.insertList(list);
         }
         //更新网校资源
         if(!oslist.isEmpty()){
            onSchoolResourceMapper.updateBigSeaScholLabel(oslist);
            List<CusDynamic> list = new ArrayList<>();
            for (OnSchoolResource os : oslist){
               CusDynamic cusde = new CusDynamic();
               cusde.setDyCreator(userid);
               cusde.setDyUuid(Utils.getUUID());
               cusde.setDySourceType(Constant.ZIYUAN_TYPE_SCHOLLE);
               cusde.setDyType(Constant.CUSTOMER_RECORD_UPDATECUSTONER);
               cusde.setDyLabelAfter(cusResource.getLabel());
               cusde.setDyLabelBefore(os.getOsLabelName());
               cusde.setDyCusUuid(os.getOsId()+"");
               cusde.setDyContent(Constant.CUSTOMER_RECORD_UPDATECUSTONER_TEXT);
               list.add(cusde);
            }
            cusDynamicMapper.insertList(list);
         }
         //更新大资源类型
         if (!biglist.isEmpty()){
            bigEmpMapper.updateBigSeaBigEmplabel(biglist);
            List<CusDynamic> list = new ArrayList<>();
            for (BigEmp bigEmp : biglist){
               CusDynamic cusde = new CusDynamic();
               cusde.setDyUuid(Utils.getUUID());
               cusde.setDyCreator(userid);
               cusde.setDySourceType(Constant.ZIYUAN_TYPE_BIGENP);
               cusde.setDyType(Constant.CUSTOMER_RECORD_UPDATECUSTONER);
               cusde.setDyLabelAfter(cusResource.getLabel());
               cusde.setDyLabelBefore(bigEmp.getBpLabelName());
               cusde.setDyCusUuid(bigEmp.getBpId()+"");
               cusde.setDyContent(Constant.CUSTOMER_RECORD_UPDATECUSTONER_TEXT);
               list.add(cusde);
            }
            cusDynamicMapper.insertList(list);
         }
         if(!mylist.isEmpty()){
            mcsMapper.updateMylabel(mylist);
            List<CusDynamic> list = new ArrayList<>();
            for (MyCustomer mycus : mylist){
               CusDynamic cusde = new CusDynamic();
               cusde.setDyUuid(Utils.getUUID());
               cusde.setDyCreator(userid);
               cusde.setDySourceType(Constant.ZIYUAN_TYPE_MINE);
               cusde.setDyType(Constant.CUSTOMER_RECORD_UPDATECUSTONER);
               cusde.setDyLabelAfter(cusResource.getLabel());
               cusde.setDyLabelBefore(mycus.getMyCusLabelName());
               cusde.setDyCusUuid(mycus.getMyCusId()+"");
               cusde.setDyContent(Constant.CUSTOMER_RECORD_UPDATECUSTONER_TEXT);
               list.add(cusde);
            }
            cusDynamicMapper.insertList(list);
         }
         if (!cAiEmps.isEmpty()){
            caiEmpMapper.updateAilabel(cAiEmps);
            List<CusDynamic> list = new ArrayList<>();
            for (CAiEmp cAiEmp : cAiEmps){
               CusDynamic cusde = new CusDynamic();
               cusde.setDyUuid(Utils.getUUID());
               cusde.setDyCreator(userid);
               cusde.setDySourceType(Constant.ZIYUAN_TYPE_AI);
               cusde.setDyType(Constant.CUSTOMER_RECORD_UPDATECUSTONER);
               cusde.setDyLabelAfter(cusResource.getLabel());
               cusde.setDyLabelBefore(cAiEmp.getAiLabelName());
               cusde.setDyCusUuid(cAiEmp.getAiId()+"");
               cusde.setDyContent(Constant.CUSTOMER_RECORD_UPDATECUSTONER_TEXT);
               list.add(cusde);
            }
            cusDynamicMapper.insertList(list);
         }

         return ResponseBo.ok();
      }catch (Exception e){
         e.printStackTrace();
         throw new RuntimeException("更新标签失败");
      }
   }

   /**解析标签内容**/
   protected StringBuilder parseLableinfo(String curentLabel,List<String> idstrids,List<String> tagid ){
      List<String> labels = Arrays.asList(curentLabel.split(","));
      List<String> uuidslabel = new ArrayList<>(labels);
      for (int j=0;j<idstrids.size();j++) {
         String idtsr = idstrids.get(j);
         if ("1".equals(idtsr)){
            //保持本组原有标签不变
         }else{
            //替换本组标签 删除原有得标签
            Example example = new Example(SaleTag.class);
            example.createCriteria().andEqualTo("tagGroupId",tagid.get(j));
            List<SaleTag> list = saleTagMapper.selectByExample(example);
            if (!StringUtils.isEmpty(curentLabel)){
               for (SaleTag saleTag:list){
                  String labelUuid = saleTag.getTagUuids();
                  uuidslabel.remove(labelUuid);
               }
            }
         }
      }
      StringBuilder sb = new StringBuilder();
      for (String uuid :uuidslabel){ sb.append(uuid+","); }
      return sb;
   }

   /**领取推广客户**/
   @Override
   @Transactional
   public ResponseBo receviceMyCustomer(CusResource cus) {
      //推广资源
      List<ExEmp> exlist = new ArrayList<>();
      List<String> lsit = new ArrayList<>();
      try {
         List<String> ids = Arrays.asList((cus.getUuid()+"").split(","));
         for(int t = 0;t < ids.size();t++){
            String  typeid = ids.get(t);
            /**查询是不是已经被领取了**/
            ExEmp exEmp = new ExEmp();
            exEmp.setcExId(Integer.valueOf(typeid));
            ExEmp selectEmp = exEmpMapper.selectOne(exEmp);
            String isRecevice = selectEmp.getcExReceiver();
            if (StringUtils.isEmpty(isRecevice)){
               exEmp.setcExReceiver(cus.getReceiver());
               exlist.add(exEmp);
               lsit.add(typeid);
            }
         }
         //更新推广资源类型
         if (!exlist.isEmpty()){
            exEmpMapper.updateExRecevice(exlist);
            CusDynamic cusde = new CusDynamic();
            cusde.setDyCreator(cus.getReceiver());
            cusde.setDySourceType(Constant.ZIYUAN_TYPE_EXEMP);
            cusde.setDyType(Constant.CUSTOMER_RECORD_LINGQU);
            cusDynamicService.inserDynamic(lsit,cusde);
         }
         return ResponseBo.ok();
      }catch (Exception e){
         return ResponseBo.error("领取部门公海客户失败："+e.getMessage());
      }
   }
   /**公司公海领取**/
   @Override
   public ResponseBo companyMyCustomerLingqu(CusResource cusResource) {
      List<ExEmp> exlist = new ArrayList<>();//推广资源
      List<String> exuuids = new ArrayList<>();

      List<OnSchoolResource> oslist = new ArrayList<>(); //高校资源
      List<String> onuuids = new ArrayList<>();

      List<BigEmp> biglist = new ArrayList<>(); // 大资源客户
      List<String> biguuids = new ArrayList<>();

      List<MyCustomer> mylist = new ArrayList<>();//我的客户
      List<String> myuuids = new ArrayList<>();

      List<CAiEmp> cAiEmps = new ArrayList<>();  //AI资源
      List<String> Aiuuid = new ArrayList<>();

      try {
         List<String>  typeids = Arrays.asList(cusResource.getTypes().split(","));
         List<String> ids = Arrays.asList((cusResource.getUuid()).split(","));

         /**再领取之前 先看看当前登陆人今天领取了多少**/
         int i = cusDynamicService.selectGongHaiResourceOverRevice(cusResource.getReceiver());
         if (i < 0)return ResponseBo.error("系统获取数据失败");
         /**判断实际已经领取的人数**/
         int allRecevice = i+ids.size();
         if (allRecevice > Constant.GONGAHI_RECEVICE_SHANGXINA){
            int sheng = Constant.GONGAHI_RECEVICE_SHANGXINA - i ;
            return ResponseBo.error("今日已经领取了 "+i+" 个客户,还有最多 "+sheng+"个可领取");
         }
         for(int t = 0;t < typeids.size();t++){
            String  typeid = typeids.get(t);
            if(Constant.ZIYUAN_TYPE_EXEMP.equals(typeid)){
               //推广资源
               ExEmp exEmp = new ExEmp();
               exEmp.setcExId(Integer.valueOf(ids.get(t)));
               exEmp.setcExReceiver(cusResource.getReceiver());
               exlist.add(exEmp);
               exuuids.add(ids.get(t));
            }else if (Constant.ZIYUAN_TYPE_SCHOLLE.equals(typeid)){
               //网校资源
               OnSchoolResource onclaole = new OnSchoolResource();
               onclaole.setOsId(Long.valueOf(ids.get(t)));
               onclaole.setOsMyCustomer(cusResource.getReceiver());
               oslist.add(onclaole);
               onuuids.add(ids.get(t));
            }else if(Constant.ZIYUAN_TYPE_BIGENP.equals(typeid)){
               //大资源类型
               BigEmp bigEmp = new BigEmp();
               bigEmp.setBpId(Long.valueOf(ids.get(t)));
               bigEmp.setBpDistribute(cusResource.getReceiver());
               biglist.add(bigEmp);
               biguuids.add(ids.get(t));
            }else if(Constant.ZIYUAN_TYPE_MINE.equals(typeid)){
               MyCustomer customer = new MyCustomer();
               customer.setMyCusId(Long.valueOf(ids.get(t)));
               customer.setMyCusReceiver(cusResource.getReceiver());
               mylist.add(customer);
               myuuids.add(ids.get(t));
            }else if (Constant.ZIYUAN_TYPE_AI.equals(typeid)){
               CAiEmp cAiEmp = new CAiEmp();
               cAiEmp.setAiId(Long.valueOf(ids.get(t)));
               cAiEmp.setAiDistriute(cusResource.getReceiver());
               cAiEmps.add(cAiEmp);
               Aiuuid.add(ids.get(t));
            }
         }
         //更新AI资源
         if (!cAiEmps.isEmpty()){
            caiEmpMapper.updateAiSeaBigEmpLingqu(cAiEmps);
            CusDynamic cusde = new CusDynamic();
            cusde.setDyCreator(cusResource.getReceiver());
            cusde.setDySourceType(Constant.ZIYUAN_TYPE_AI);
            cusde.setDyType(Constant.CUSTOMER_RECORD_GONGHAILINGQU);
            cusde.setDyDisUserid(cusResource.getReceiver());
            cusDynamicService.inserDynamic(Aiuuid,cusde);
         }

         //更新推广资源类型
         if (!exlist.isEmpty()){
            exEmpMapper.updateBigExLingQu(exlist);
            CusDynamic cusde = new CusDynamic();
            cusde.setDyCreator(cusResource.getReceiver());
            cusde.setDySourceType(Constant.ZIYUAN_TYPE_EXEMP);
            cusde.setDyType(Constant.CUSTOMER_RECORD_GONGHAILINGQU);
            cusde.setDyDisUserid(cusResource.getReceiver());
            cusDynamicService.inserDynamic(exuuids,cusde);
         }
         //更新网校资源
         if(!oslist.isEmpty()){
            onSchoolResourceMapper.updateBigSeaScholLingQu(oslist);
            CusDynamic cusde = new CusDynamic();
            cusde.setDyCreator(cusResource.getReceiver());
            cusde.setDySourceType(Constant.ZIYUAN_TYPE_SCHOLLE);
            cusde.setDyType(Constant.CUSTOMER_RECORD_GONGHAILINGQU);
            cusde.setDyDisUserid(cusResource.getReceiver());
            cusDynamicService.inserDynamic(onuuids,cusde);
         }
         //更新大资源类型
         if (!biglist.isEmpty()){
            bigEmpMapper.updateBigSeaBigEmpLingqu(biglist);
            CusDynamic cusde = new CusDynamic();
            cusde.setDyCreator(cusResource.getReceiver());
            cusde.setDySourceType(Constant.ZIYUAN_TYPE_BIGENP);
            cusde.setDyType(Constant.CUSTOMER_RECORD_GONGHAILINGQU);
            cusde.setDyDisUserid(cusResource.getReceiver());
            cusDynamicService.inserDynamic(biguuids,cusde);
         }
         if(!mylist.isEmpty()){
            mcsMapper.updateBigMyCusLingqu(mylist);
            CusDynamic cusde = new CusDynamic();
            cusde.setDyCreator(cusResource.getReceiver());
            cusde.setDySourceType(Constant.ZIYUAN_TYPE_MINE);
            cusde.setDyType(Constant.CUSTOMER_RECORD_GONGHAILINGQU);
            cusde.setDyDisUserid(cusResource.getReceiver());
            cusDynamicService.inserDynamic(myuuids,cusde);
         }
         return ResponseBo.ok("领取成功");
      }catch (Exception e){
         e.printStackTrace();
         return ResponseBo.error("领取失败");
      }
   }
   /**公司大公海的重新分配**/
   @Override
   public ResponseBo companyMyCustomerDistore(CusResource cusResource) {
      List<ExEmp> exlist = new ArrayList<>();//推广资源
      List<String> exuuids = new ArrayList<>();

      List<OnSchoolResource> oslist = new ArrayList<>(); //高校资源
      List<String> onuuids = new ArrayList<>();

      List<BigEmp> biglist = new ArrayList<>(); // 大资源客户
      List<String> biguuids = new ArrayList<>();

      List<MyCustomer> mylist = new ArrayList<>();//我的客户
      List<String> myuuids = new ArrayList<>();

      List<CAiEmp> cAiEmps = new ArrayList<>(); //Ai资源
      List<String> cuuid = new ArrayList<>();

      try {
         List<String> uuidids = Arrays.asList(cusResource.getUuid().split(","));
         List<String>  typeids = Arrays.asList(cusResource.getTypes().split(","));
         List<String> ids = Arrays.asList((cusResource.getIds()).split(","));
         for(int t = 0;t < typeids.size();t++){
            String  typeid = typeids.get(t);
            if (Constant.ZIYUAN_TYPE_AI.equals(typeid)){
               //Ai资源
               CAiEmp cAiEmp = new CAiEmp();
               cAiEmp.setAiId(Long.valueOf(ids.get(t)));
               cAiEmp.setAiRUuid(uuidids.get(t));
               cAiEmp.setAiDistriute(cusResource.getReceiver());
               cAiEmp.setAiDisPerson(cusResource.getDistributor());
               cAiEmps.add(cAiEmp);
               cuuid.add(ids.get(t));
            }else if(Constant.ZIYUAN_TYPE_EXEMP.equals(typeid)){
               //推广资源
               ExEmp exEmp = new ExEmp();
               exEmp.setcExId(Integer.valueOf(ids.get(t)));
               exEmp.setcExUuid(uuidids.get(t));
               exEmp.setcExReceiver(cusResource.getReceiver());
               exEmp.setcExCreator(cusResource.getDistributor());
               exlist.add(exEmp);
               exuuids.add(ids.get(t));
            }else if (Constant.ZIYUAN_TYPE_SCHOLLE.equals(typeid)){
               //网校资源
               OnSchoolResource onclaole = new OnSchoolResource();
               onclaole.setOsId(Long.valueOf(ids.get(t)));
               onclaole.setOsUuid(uuidids.get(t));
               onclaole.setOsMyCustomer(cusResource.getReceiver());
               onclaole.setOsDistributor(cusResource.getDistributor());
               oslist.add(onclaole);
               onuuids.add(ids.get(t));
            }else if(Constant.ZIYUAN_TYPE_BIGENP.equals(typeid)){
               //大资源类型
               BigEmp bigEmp = new BigEmp();
               bigEmp.setBpRUuid(uuidids.get(t));
               bigEmp.setBpId(Long.valueOf(ids.get(t)));
               bigEmp.setBpDistribute(cusResource.getReceiver());
               bigEmp.setBpDisPerson(cusResource.getDistributor());
               biglist.add(bigEmp);
               biguuids.add(ids.get(t));
            }else if(Constant.ZIYUAN_TYPE_MINE.equals(typeid)){
               MyCustomer customer = new MyCustomer();
               customer.setMyCusId(Long.valueOf(ids.get(t)));
               customer.setMyCusUuid(uuidids.get(t));
               customer.setMyCusReceiver(cusResource.getReceiver());
               customer.setMyCusCreator(cusResource.getDistributor());
               mylist.add(customer);
               myuuids.add(ids.get(t));
            }
         }
         //更新AI资源类型
         if (!cAiEmps.isEmpty()){
            caiEmpMapper.updateAiExDustore(cAiEmps);
            CusDynamic cusde = new CusDynamic();
            cusde.setDyCreator(cusResource.getDistributor());
            cusde.setDySourceType(Constant.ZIYUAN_TYPE_AI);
            cusde.setDyType(Constant.CUSTOMER_RECORD_GONGHAIFENPEI);
            cusde.setDyDisUserid(cusResource.getReceiver());
            cusDynamicService.inserDynamic(cuuid,cusde);
         }
         //更新推广资源类型
         if (!exlist.isEmpty()){
            exEmpMapper.updateBigExDustore(exlist);
            CusDynamic cusde = new CusDynamic();
            cusde.setDyCreator(cusResource.getDistributor());
            cusde.setDySourceType(Constant.ZIYUAN_TYPE_EXEMP);
            cusde.setDyType(Constant.CUSTOMER_RECORD_GONGHAIFENPEI);
            cusde.setDyDisUserid(cusResource.getReceiver());
            cusDynamicService.inserDynamic(exuuids,cusde);
         }
         //更新网校资源
         if(!oslist.isEmpty()){
            onSchoolResourceMapper.updateBigSeaSchol(oslist);
            CusDynamic cusde = new CusDynamic();
            cusde.setDyCreator(cusResource.getDistributor());
            cusde.setDySourceType(Constant.ZIYUAN_TYPE_SCHOLLE);
            cusde.setDyType(Constant.CUSTOMER_RECORD_GONGHAIFENPEI);
            cusde.setDyDisUserid(cusResource.getReceiver());
            cusDynamicService.inserDynamic(onuuids,cusde);
         }
         //更新大资源类型
         if (!biglist.isEmpty()){
            bigEmpMapper.updateBigSeaBigEmp(biglist);
            CusDynamic cusde = new CusDynamic();
            cusde.setDyCreator(cusResource.getDistributor());
            cusde.setDySourceType(Constant.ZIYUAN_TYPE_BIGENP);
            cusde.setDyType(Constant.CUSTOMER_RECORD_GONGHAIFENPEI);
            cusde.setDyDisUserid(cusResource.getReceiver());
            cusDynamicService.inserDynamic(biguuids,cusde);
         }
         if(!mylist.isEmpty()){
            mcsMapper.updateBigMyCus(mylist);
            CusDynamic cusde = new CusDynamic();
            cusde.setDyCreator(cusResource.getDistributor());
            cusde.setDySourceType(Constant.ZIYUAN_TYPE_MINE);
            cusde.setDyType(Constant.CUSTOMER_RECORD_GONGHAIFENPEI);
            cusde.setDyDisUserid(cusResource.getReceiver());
            cusDynamicService.inserDynamic(myuuids,cusde);
         }
         return ResponseBo.ok();
      }catch (Exception e){
         return ResponseBo.error();
      }
   }
   /**导出客户资源**/
   @Override
   public List<CusResource> exportBigEmpInfo(String exportids) {
      String []exportsIds  = exportids.split(",");
      List<String> ids = new ArrayList<>();
      for(String id:exportsIds){ ids.add(id); }
      Example example = new Example(CusResource.class);
      example.createCriteria().andIn("uuid",ids);
      return  this.selectByExample(example);
   }


   /**我的客户移动分组*/
   public ResponseBo moveCusGroup(CusResource cusResource){
      //推广资源
      List<ExEmp> exlist = new ArrayList<>();
      List<OnSchoolResource> oslist = new ArrayList<>(); //高校资源
      List<BigEmp> biglist = new ArrayList<>(); // 大资源客户
      List<MyCustomer> mylist = new ArrayList<>();//我的客户
      List<CAiEmp> aiEmps = new ArrayList<>();  //Ai资源类
      try{
         List<String> uuidids = Arrays.asList(cusResource.getUuid().split(","));
         List<String>  typeids = Arrays.asList(cusResource.getTypes().split(","));
         List<String> ids = Arrays.asList((cusResource.getIds()).split(","));
         for(int t = 0;t < typeids.size();t++) {
            String typeid = typeids.get(t);
            if (Constant.ZIYUAN_TYPE_EXEMP.equals(typeid)) {
               //推广资源
               ExEmp exEmp = new ExEmp();
               exEmp.setcExId(Integer.valueOf(ids.get(t)));
               exEmp.setcExUuid(uuidids.get(t));
               exEmp.setcExGroup(cusResource.getGroups());
               exlist.add(exEmp);
            } else if (Constant.ZIYUAN_TYPE_SCHOLLE.equals(typeid)) {
               //网校资源
               OnSchoolResource onclaole = new OnSchoolResource();
               onclaole.setOsId(Long.valueOf(ids.get(t)));
               onclaole.setOsUuid(uuidids.get(t));
               onclaole.setOsGroups(cusResource.getGroups());
               oslist.add(onclaole);
            } else if (Constant.ZIYUAN_TYPE_BIGENP.equals(typeid)) {
               //大资源类型
               BigEmp bigEmp = new BigEmp();
               bigEmp.setBpRUuid(uuidids.get(t));
               bigEmp.setBpId(Long.valueOf(ids.get(t)));
               bigEmp.setBpGroup(cusResource.getGroups());
               biglist.add(bigEmp);
            }else if(Constant.ZIYUAN_TYPE_MINE.equals(typeid)){
               MyCustomer customer = new MyCustomer();
               customer.setMyCusUuid(uuidids.get(t));
               customer.setMyCusId(Long.valueOf(ids.get(t)));
               customer.setMyCusGroup(cusResource.getGroups());
               mylist.add(customer);
            }else if (Constant.ZIYUAN_TYPE_AI.equals(typeid)){
               CAiEmp cAiEmp = new CAiEmp();
               cAiEmp.setAiRUuid(uuidids.get(t));
               cAiEmp.setAiId(Long.valueOf(ids.get(t)));
               cAiEmp.setAiGroup(cusResource.getGroups());
               aiEmps.add(cAiEmp);
            }
         }
         //更新推广资源类型
         if (!exlist.isEmpty()){
            exEmpMapper.moveExCusGroup(exlist);
         }
         //更新网校资源
         if(!oslist.isEmpty()){
            onSchoolResourceMapper.moveOnSchoolCusGroup(oslist);
         }
         //更新大资源类型
         if (!biglist.isEmpty()){
            bigEmpMapper.moveBigCusGroup(biglist);
         }
         //更新我的客户
         if(!mylist.isEmpty()){
            mcsMapper.moveMyGroup(mylist);
         }
         //更新Ai资源
         if (!aiEmps.isEmpty()){
            caiEmpMapper.moveAiCusGroup(aiEmps);
         }
         return ResponseBo.ok();
      }catch (Exception e){
         e.printStackTrace();
         return ResponseBo.error();
      }
   }

   /**客户查重*/
   @Override
   public List<CusResource> checkCustomer(CusResource cus){
      return  cusResourceMapper.selectCkeckData(cus);
   }
   /**通过手机查询重复详情列表*/
   @Override
   public List<CusResource> checkCustomerByMobile(CusResource cus){
      return cusResourceMapper.selectCkeckDetail(cus);
   }

   @Override
   public List<EmpFenPei> exportAllFenEmpInfo(String types, String uuids) {

      List<EmpFenPei> exlist = new ArrayList<>();//资源

      List<String> caiuuids = new ArrayList<>();
      List<String> school = new ArrayList<>();

      List<String> bigEmp = new ArrayList<>();
      try {
         List<String> typeids = Arrays.asList(types.split(","));
         List<String> ids = Arrays.asList(uuids.split(","));
         for (int t = 0; t < typeids.size(); t++) {
            String typeid = typeids.get(t);
            if (Constant.ZIYUAN_TYPE_AI.equals(typeid)) {
               //Ai资源
               caiuuids.add(ids.get(t));
            } else if (Constant.ZIYUAN_TYPE_SCHOLLE.equals(typeid)) {
               //网校资源
               school.add(ids.get(t));
            } else if (Constant.ZIYUAN_TYPE_BIGENP.equals(typeid)) {
               //大资源类型
               bigEmp.add(ids.get(t));
            }
         }
         List<MyUser> users = userMapper.selectAll();
         if (!caiuuids.isEmpty()){
            List<CAiEmp> cAiEmps = caiEmpMapper.selectExportAiList(caiuuids);
            EmpFenPei fenPei = null;
            for (CAiEmp cAiEmp : cAiEmps){
               fenPei = new EmpFenPei();
               fenPei.setExName( cAiEmp.getAiName());
               fenPei.setMobile(cAiEmp.getAiMobile());
               fenPei.setInterGroud(cAiEmp.getAiGroud());
               fenPei.setZone(cAiEmp.getAiZone());
               fenPei.setLabel(cAiEmp.getAiLabel());
               fenPei.setGenjinren(cAiEmp.getAiDistriute());
               for (MyUser user: users){
                  if (user.getUserUuid().equals(cAiEmp.getAiDistriute())){
                     fenPei.setGenjinren(user.getNickname());
                     break;
                  }
               }
               exlist.add(fenPei);
            }
         }
         if (!school.isEmpty()){
            List<OnSchoolResource> onSchoolResources = onSchoolResourceMapper.selectExportschoolEmpList(school);
            EmpFenPei fenPei = null;
            for (OnSchoolResource schooll : onSchoolResources){
               fenPei = new EmpFenPei();
               fenPei.setExName( schooll.getOsNickname());
               fenPei.setMobile(schooll.getOsMobile());
               fenPei.setInterGroud(schooll.getOsGroud());
               fenPei.setZone(schooll.getOsZone());
               fenPei.setLabel(schooll.getOsLabel());
               fenPei.setExAge(schooll.getOsSex());
               fenPei.setQq(schooll.getOsQq());
               fenPei.setInterGroud(schooll.getOsGroud());
               fenPei.setPianZone(schooll.getOsCity());
               for (MyUser user: users){
                  if (user.getUserUuid().equals(schooll.getOsMyCustomer())){
                     fenPei.setGenjinren(user.getNickname());
                     break;
                  }
               }
               exlist.add(fenPei);
            }

         }
         if (!bigEmp.isEmpty()){
            List<BigEmp> bigEmps = bigEmpMapper.selectExportBigEmpList(bigEmp);

            EmpFenPei fenPei = null;
            for (BigEmp bigEmpinfo : bigEmps){
               fenPei = new EmpFenPei();
               fenPei.setExName( bigEmpinfo.getBpName());
               fenPei.setMobile(bigEmpinfo.getBpMobile());
               fenPei.setInterGroud(bigEmpinfo.getBpGroud());
               fenPei.setZone(bigEmpinfo.getBpZone());
               fenPei.setLabel(bigEmpinfo.getBpLabel());
               fenPei.setExAge(bigEmpinfo.getBpSex());
               fenPei.setQq(bigEmpinfo.getBpQq());
               fenPei.setInterGroud(bigEmpinfo.getBpGroud());
               fenPei.setPianZone(bigEmpinfo.getBpCity());
               fenPei.setInterProject(bigEmpinfo.getBpInteProject());
               fenPei.setInterSource(bigEmpinfo.getBpSourChanel());
               fenPei.setCollege(bigEmpinfo.getBpColleges());
               fenPei.setAcademy(bigEmpinfo.getBpAcademy());
               fenPei.setMajor(bigEmpinfo.getBpMajor());
               fenPei.setYear(bigEmpinfo.getBpYear());
               for (MyUser user: users){
                  if (user.getUserUuid().equals(bigEmpinfo.getBpDistribute())){
                     fenPei.setGenjinren(user.getNickname());
                     break;
                  }
               }
               exlist.add(fenPei);
            }

         }
      } catch (Exception e) {
         log.error("获取分配数据失败:"+e.getMessage());
      }
      return  exlist;

   }
}
